package ${content.controller.classPackage};

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ${content.assist.classPackage}.BaseController;
import ${content.assist.classPackage}.ExecuteResult;
import ${content.assist.classPackage}.Pager;
import ${content.entity.classPackage}.${content.entity.className};
import ${content.service.classPackage}.${content.service.className};

@RestController
@RequestMapping("/admin/${content.table.tableName}")
public class ${content.controller.className} extends BaseController {

	@Autowired
	private ${content.service.className} ${content.service.className?uncap_first};

	/**
	 * @Desc: 列表
	 * @param page
	 * @param ${content.entity.className?uncap_first}
	 * @return
	 */
	@PostMapping(value = "/page")
	public ExecuteResult<Pager<${content.entity.className}>> page(Pager<${content.entity.className}> page, ${content.entity.className} ${content.entity.className?uncap_first}) {

		ExecuteResult<Pager<${content.entity.className}>> pageList = ${content.service.className?uncap_first}.getPageByCondition(page, ${content.entity.className?uncap_first});
		return pageList;
	}

	/**
	 * 添加或编辑
	 * 
	 * @param ${content.entity.className?uncap_first}
	 * @param bindingResult
	 * @return
	 */
	@PostMapping(value = "edit")
	public ExecuteResult<${content.entity.className}> edit(@Validated ${content.entity.className} ${content.entity.className?uncap_first}, BindingResult bindingResult) {

		ExecuteResult<${content.entity.className}> result = new ExecuteResult<>();

		if (${content.entity.className?uncap_first} == null) {
			result.addErrorMessage("信息不能为空！");
			return result;
		}

		if (bindingResult.hasErrors()) {
			this.bindingFieldErrors(result, bindingResult);
			return result;
		}

		if (${content.entity.className?uncap_first}.getId() == null) {
			${content.service.className?uncap_first}.insert(${content.entity.className?uncap_first});
		} else {
			${content.service.className?uncap_first}.update(${content.entity.className?uncap_first});
		}

		return result;
	}

	/**
	 * 信息
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping(value = "info")
	public ExecuteResult<${content.entity.className}> info(@RequestParam Integer id) {

		ExecuteResult<${content.entity.className}> result = new ExecuteResult<>();

		if (id == null) {
			result.addErrorMessage("id不能为空！");
			return result;
		}

		result = ${content.service.className?uncap_first}.getOneById(id);

		return result;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	@PostMapping(value = "delete")
	public ExecuteResult<${content.entity.className}> delete(@RequestParam Integer id) {

		ExecuteResult<${content.entity.className}> result = new ExecuteResult<>();

		if (StringUtils.isEmpty(id)) {
			result.addErrorMessage("id不能为空！");
			return result;
		}

		${content.entity.className} ${content.entity.className?uncap_first} = new ${content.entity.className}();
		${content.entity.className?uncap_first}.setId(id);
		${content.service.className?uncap_first}.delete(${content.entity.className?uncap_first});

		return result;
	}
}
